#include "updatepage.h"
#include "version.h"

#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QDebug>
#include <QProcess>
#include <QAuthenticator>

//==============================================================================
// Define the URL of the Update Definitions file
//==============================================================================

//static const QString DEFS_URL = "https://gitee.com/ZhangJieHua/application/raw/master/application/src/gui/update/updates.json";
static const QString DEFS_URL = "http://192.168.0.138/svn/multipump/software/infusion/trunk/upgrade/updates_img/updates.json";

UpdatePage::UpdatePage(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("UpdatePage"));
    setAttribute(Qt::WA_DeleteOnClose, true);

    /* QSimpleUpdater is single-instance */
    m_updater = QSimpleUpdater::getInstance();

    /* Check for updates when the "Check For Updates" button is clicked */
    connect(m_updater, SIGNAL(checkingFinished(QString)), this, SLOT(updateChangelog(QString)));
//    connect(m_updater, SIGNAL(appcastDownloaded(QString, QByteArray)), this, SLOT(displayAppcast(QString, QByteArray)));
    connect(m_updater, SIGNAL(downloadFinished(QString, QString)), this, SLOT(downloadFinished(QString, QString)));

    m_updater->setDownloadDir(DEFS_URL, QDir::homePath() + "/Downloads/");
    // 使用自定义安装程序
    m_updater->setUseCustomInstallProcedures(DEFS_URL, true);

    // 控件
    QLabel *lbVersion = new QLabel(tr("version : "));
    m_lbVersion = new QLabel();
    m_lbVersion->setText(APP_VERSION);
    QHBoxLayout *hblVersion = new QHBoxLayout();
    hblVersion->addWidget(lbVersion);
    hblVersion->addWidget(m_lbVersion);

    QLabel *lbLatestVersion = new QLabel(tr("Latest version : "));
    m_lbLatestVersion = new QLabel();
    QHBoxLayout *hblLastestVersion = new QHBoxLayout();
    hblLastestVersion->addWidget(lbLatestVersion);
    hblLastestVersion->addWidget(m_lbLatestVersion);

    m_changelogText = new QTextBrowser();

    m_pbCheckForUpdate = new QPushButton();
    m_pbCheckForUpdate->setText(tr("CheckForUpdate"));
    connect(m_pbCheckForUpdate, &QPushButton::clicked, this, &UpdatePage::checkForUpdates);

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(hblVersion);
    vbl->addLayout(hblLastestVersion);
    vbl->addWidget(m_changelogText);
    vbl->addWidget(m_pbCheckForUpdate);
    setLayout(vbl);
}

void UpdatePage::checkForUpdates()
{
   /* Apply the settings */
   m_updater->setModuleName(DEFS_URL, "Application");
   m_updater->setModuleVersion(DEFS_URL, APP_VERSION);
   m_updater->setNotifyOnFinish(DEFS_URL, true);
   m_updater->setNotifyOnUpdate(DEFS_URL, true);
   m_updater->setUseCustomAppcast(DEFS_URL, false);
   m_updater->setDownloaderEnabled(DEFS_URL, true);
   m_updater->setMandatoryUpdate(DEFS_URL, false);

   m_updater->setUserAndPassword(DEFS_URL, "zhangjiehua", "123456");
//   m_updater->setUserAndPassword(DEFS_URL, "cvsftp", "cvsftp");

   /* Check for updates */
   m_updater->checkForUpdates(DEFS_URL);
}

//==============================================================================
// Window::updateChangelog
//==============================================================================

void UpdatePage::updateChangelog(const QString &url)
{
   if (url == DEFS_URL)
   {
      m_changelogText->setText(m_updater->getChangelog(url));

   }
}

//==============================================================================
// Window::displayAppcast
//==============================================================================

void UpdatePage::displayAppcast(const QString &url, const QByteArray &reply)
{
   if (url == DEFS_URL)
   {
      QString text = "This is the downloaded appcast: <p><pre>" + QString::fromUtf8(reply)
          + "</pre></p><p> If you need to store more information on the "
            "appcast (or use another format), just use the "
            "<b>QSimpleUpdater::setCustomAppcast()</b> function. "
            "It allows your application to interpret the appcast "
            "using your code and not QSU's code.</p>";

      m_changelogText->setText(text);
   }
}

void UpdatePage::downloadFinished(const QString &url, const QString &filepath)
{
    qDebug() << filepath;

    QProcess *p = new QProcess();
    connect(p, SIGNAL(finished(int)), p, SLOT(deleteLater()));
    connect(p, &QProcess::destroyed, this, [=](){

    });

    QStringList args;
    args << filepath << "/home/jiehua/";
    p->start("cp", args);
}
